package org.limit.bookServe.bean;

import org.limit.bookServe.enums.UserIdentityEnum;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

import static org.limit.bookServe.bean.User.UserAccStatus.NORMAL;
import static org.limit.bookServe.enums.UserIdentityEnum.COMMON;
import static org.limit.bookServe.utils.SystemConstants.DATE_PATTERN;

/**
 * @author 1.
 * @version 1.0
 * @description: 用户类
 * @date 2023/11/1 19:29
 */
public class User implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    //唯一id：hash值作为唯一id
    private String uid;
    //用户名
    private String userName;
    //密码，默认：12345
    private String passWord;
    //用户身份，默认【普通】
    private UserIdentityEnum identity;
    //注册时间
    private Date registerTime;
    //用户状态  正常 禁用 注销
    private UserAccStatus status;


    public User(String userName, String password) {
        this.userName = userName;
        this.passWord = password;
        this.identity = COMMON;
        this.registerTime = new Date();
        this.status = NORMAL;
        this.uid = String.valueOf(hashCode());
    }

    public User(String userName) {
        this.userName = userName;
        //默认密码：12345
        this.passWord = "12345";
        this.identity = COMMON;
        this.registerTime = new Date();
        this.status = NORMAL;
        this.uid = String.valueOf(hashCode());
    }

    // 用户账户状态枚举
    public enum UserAccStatus {
        NORMAL(1, "正常"),
        DISABLE(2, "禁用"),
        LOGOUT(3, "注销"),
        ;

        private int index;
        private String desc;

        UserAccStatus(int index, String desc) {
            this.index = index;
            this.desc = desc;
        }

        public int getIndex() {
            return index;
        }

        public String getDesc() {
            return desc;
        }
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public UserIdentityEnum getIdentity() {
        return identity;
    }

    public void setIdentity(UserIdentityEnum identity) {
        this.identity = identity;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    public UserAccStatus getStatus() {
        return status;
    }

    public void setStatus(UserAccStatus status) {
        this.status = status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(uid, user.uid) && Objects.equals(userName, user.userName) && Objects.equals(passWord, user.passWord) && identity == user.identity && Objects.equals(registerTime, user.registerTime) && status == user.status;
    }

    @Override
    public int hashCode() {
        return Objects.hash(userName, passWord, identity, registerTime, status);
    }

    @Override
    public String toString() {
        return "User { 用户id: " + this.uid + ", 用户名: " + this.userName + ", 用户身份: " + this.identity.getDesc()
             + ", 账号状态: " + this.status.getDesc() + ", 注册时间: " + DATE_PATTERN.format(this.registerTime) + " }";
    }
}
